package cn.com.yinhex.as.util;

public class Pages {
	public static final String splitPages(int thePage, String queryString, int count, int pageLength) {
		int maxPage = count / pageLength + 1;
		int prevPage = thePage - 1;
		if (prevPage < 1) {
			prevPage = 1;
		}
		int nextPage = thePage + 1;

		int showMaxPage = thePage + 6;
		int spacePage = 1;

		if (thePage > 6) {
			spacePage = thePage - 5;
		}
		if (showMaxPage < 10) {
			showMaxPage = 10;
		}

		StringBuffer sb = new StringBuffer();
		sb.append("<table cellspacing=0 cellpadding=3 border=0 align=center style=\"line-height:22px\">");
		sb.append("<tr><td align=\"right\"><a href=\"" + queryString + "/" + prevPage + "\"><font face=\"Webdings\">7</font>��һҳ</a></td><td>");
		/*
		 * sb.append("<a href=\"" + queryString + "&pagex=" + prevPage +
		 * "\"><font face=\"Webdings\">7</font>��һҳ</a> </td>");
		 */
		for (int i = spacePage; i < showMaxPage && i <= maxPage; i++) {
			String fontColor = "black";
			if (i == thePage) {
				fontColor = "red";
			}
			sb.append("<a href=\"" + queryString + "/" + i + "\" class='num'><font color=" + fontColor + ">" + StringUtil.addzero(i, 2)
					+ "</font></a>");
		}
		sb.append("</td><td align=\"left\"><a href=\"" + queryString + "/" + nextPage + "\">��һҳ<font face=\"Webdings\">8</font></a>"
				+ "</td></tr></table>");
		return sb.toString();
	}

	public static final String splitPages2(int thePage, String queryString, int count, int pageLength) {
		int maxPage = count / pageLength + 1;
		int prevPage = thePage - 1;
		if (prevPage < 1) {
			prevPage = 1;
		}
		int nextPage = thePage + 1;

		int showMaxPage = thePage + 6;
		int spacePage = 1;

		if (thePage > 6) {
			spacePage = thePage - 5;
		}
		if (showMaxPage < 10) {
			showMaxPage = 10;
		}

		StringBuffer sb = new StringBuffer();
		sb.append("<table cellspacing=0 cellpadding=3 border=0 align=center style=\"line-height:22px\">");
		sb.append("<tr><td align=\"right\">");

		sb.append("<a href=\"" + queryString + "&pagex=" + prevPage + "\"><font face=\"Webdings\">7</font>��һҳ</a> ");
		/*
		 * sb.append("<a href=\"" + queryString + "&pagex=" + prevPage +
		 * "\"><font face=\"Webdings\">7</font>��һҳ</a> </td>");
		 */
		for (int i = spacePage; i < showMaxPage && i <= maxPage; i++) {
			String fontColor = "black";
			if (i == thePage) {
				fontColor = "red";
			}
			sb.append("<td align=\"center\"><a href=\"" + queryString + "&pagex=" + i + "\"><font color=" + fontColor + ">"
					+ StringUtil.addzero(i, 2) + "</font></a></td>");
		}
		sb.append("<td align=\"left\"><a href=\"" + queryString + "&pagex=" + nextPage + "\">��һҳ<font face=\"Webdings\">8</font></a>");
		/*
		 * sb.append(" <a href=\"" + queryString + "&pagex=" + maxPage +
		 * "\">���ҳ</a>");
		 */
		sb.append("</td></tr></table>");
		return sb.toString();
	}

	public static String splitPages3(int thePage, String queryString, int count, int pageLength, String classcolor, String hightclasscolor) {
		int numPages = (count - 1) / pageLength + 1;
		int start = thePage * pageLength - pageLength;
		int range = pageLength;
		int numMessages = count;

		StringBuffer buf = new StringBuffer();

		// Only show the pages if there is greater than one page
		if (numPages > 1) {

			// Print out a "<<" if necessary
			if (start > 0) {
				buf.append("<a href=\"" + queryString + "&pagex=" + (thePage - 1) + "\" class=\"" + classcolor + "\" target=\"_self\" title=\"��һҳ\">��һҳ</a> ");
			}

			int currentPage = (start / range) + 1;
			int lo = currentPage - 3;
			if (lo <= 0) {
				lo = 1;
			}
			int hi = currentPage + 4;

			// ���ص�һҳ
			if (lo > 1) {
				buf.append("<a href=\"" + queryString + "&pagex=1\" class=\"" + classcolor + "\" target=\"_self\">1</a><span class=\"dot\">...</span>");
			}

			// Print out low page numbers
			while (lo < currentPage) {
				buf.append("<a href=\"" + queryString + "&pagex=" + lo + "\" class=\"" + classcolor + "\" target=\"_self\">" + lo + "</a>");
				lo++;
			}
			// Current page
			buf.append("<span class=\"" + hightclasscolor + "\" >");
			buf.append(currentPage).append("</span>");
			// Print out high page numbers
			while ((currentPage < hi) && (currentPage < numPages)) {
				buf.append("<a href=\"" + queryString + "&pagex=" + (currentPage + 1) + "\" class=\"" + classcolor + "\" target=\"_self\">" + (currentPage + 1)
						+ "</a>");
				currentPage++;
			}

			// put ending page at the end, ie: " 2 3 4 ... 33"
			if (numPages > currentPage) {
				buf.append("<span class=\"dot\">...</span><a href=\"" + queryString + "&pagex=" + numPages + "\" class=\"" + classcolor + "\" target=\"_self\">"
						+ numPages + "</a>");
			}

			if (numMessages > (start + range)) {
				// int numRemaining = (int) (numMessages - (start + range));
				buf.append(" <a href=\"" + queryString + "&pagex=" + (thePage + 1) + "\" title=\"��һҳ\" class=\"" + classcolor + "\" target=\"_self\">��һҳ</a>");
			}
		}
		return buf.toString();
	}

	public static String splitPages4(int thePage, String queryString, int count, int pageLength, String classcolor, String hightclasscolor) {
		int numPages = (count - 1) / pageLength + 1;
		int start = thePage * pageLength - pageLength;
		int range = pageLength;
		int numMessages = count;

		StringBuffer buf = new StringBuffer();

		// Only show the pages if there is greater than one page
		if (numPages > 1) {

			// Print out a "<<" if necessary
			if (start > 0) {
				if (thePage - 1 > 1) {
					buf.append("<a href=\"" + queryString + "/" + (thePage - 1));
				} else {
					buf.append("<a href=\"" + queryString);
				}
				buf.append("\" class=\"" + classcolor + "\" title=\"��һҳ\" target=\"_self\">��һҳ</a> ");
			}

			int currentPage = (start / range) + 1;
			int lo = currentPage - 3;
			if (lo <= 0) {
				lo = 1;
			}
			int hi = currentPage + 4;

			// ���ص�һҳ
			if (lo > 1) {
				buf.append("<a href=\"" + queryString + "\" class=\"" + classcolor + "\" target=\"_self\">1</a><span class=\"dot\">...</span>");
			}

			// Print out low page numbers
			while (lo < currentPage) {
				buf.append("<a href=\"" + queryString);
				if (lo > 1) {
					buf.append("/").append(lo);
				}
				buf.append("\" class=\"" + classcolor + "\" target=\"_self\">" + lo + "</a>");
				lo++;
			}
			// Current page

			buf.append("<span class=\"" + hightclasscolor + "\" >" + currentPage + "</span>");
			// Print out high page numbers
			while ((currentPage < hi) && (currentPage < numPages)) {
				buf.append("<a href=\"" + queryString + "/" + (currentPage + 1) + "\" class=\"" + classcolor + "\" target=\"_self\">" + (currentPage + 1) + "</a>");
				currentPage++;
			}

			// put ending page at the end, ie: " 2 3 4 ... 33"
			if (numPages > currentPage) {
				buf.append("<span class=\"dot\">...</span><a href=\"" + queryString + "/" + numPages + "\" class=\"" + classcolor + "\" target=\"_self\">" + numPages
						+ "</a>");
			}

			if (numMessages > (start + range)) {
				// int numRemaining = (int) (numMessages - (start + range));
				buf.append(" <a href=\"" + queryString + "/" + (thePage + 1) + "\" title=\"��һҳ\" class=\"" + classcolor + "\" target=\"_self\">��һҳ</a>");
			}
		}
		return buf.toString();
	}
}
